package com.smart.manager.union.mall.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.union.mall.body.RUnionMallGoods;
import com.smart.manager.union.mall.models.UnionMallGoods;
import com.smart.manager.union.mall.repository.UnionMallGoodsRepository;
import com.smart.manager.union.mall.repository.UnionMallGoodsRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * 狼山鸡蛋兑换商品资料
 * @author Administrator
 */
@Api(tags = "实物商品资料")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "union/mall/goods")
@RequiredArgsConstructor


public class UnionMallGoodsApi {

    private final UnionMallGoodsRepository repository;

    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询商品资料")
    @SaCheckPermission("union:mall:goods:pageList")
    public AjaxResult<Page<UnionMallGoods>> pageList(@Validated RUnionMallGoods.SearchBody body) {
        Page<UnionMallGoods> result = repository.pageList(body);
        return AjaxResult.data(result);
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增商品资料")
    @SaCheckPermission("union:mall:goods:add")
    public AjaxResult<String> add(@RequestBody @Validated RUnionMallGoods.EditBody body) {
        repository.add(body);
        return AjaxResult.success();
    }


    @PutMapping(value = "update/{id}")
    @ApiOperation(value = "修改商品资料")
    @SaCheckPermission("union:mall:goods:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RUnionMallGoods.EditBody body) {
        repository.update(id, body);
        return AjaxResult.success();
    }


    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除商品资料")
    @SaCheckPermission("union:mall:goods:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        int x = repository.delByIds(ids);
        return AjaxResult.delSuccess(x);
    }

}
